﻿using System;
using OrderingPlatform.Infrastructure;
using OrderingPlatform.Model;
using OrderingPlatform.Model.UserManage;
using OrderingPlatform.DalService.UserManage;
using OrderingPlatform.MvcExtension;

namespace OrderingPlatform.BllService.UserService
{
    public class UserBaeService
    {
        private readonly UserDal _userDal = new UserDal();

        /// <summary>
        /// <para> 功    能： 获取当前登录用户名称 </para>
        /// <para> 作    者： 韩保新 </para>
        /// <para> 创建日期： 2011-11-8</para>
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserCode()
        {
            try
            {
                return UserDal.GetCurrentUserCode();
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// <para> 功    能： 获取当前如何ID </para>
        /// <para> 作    者： 韩保新 </para>
        /// <para> 创建日期： 2011-11-8</para>
        /// </summary>
        /// <returns></returns>
        public static Guid GetCurrentUserId()
        {
            try
            {
                return UserDal.GetCurrentUserId();
            }
            catch
            {
                return Guid.Empty;
            }
        }

        /// <summary>
        /// <para> 功    能： 获取当前用户集合信息 </para>
        /// <para> 作    者： 韩保新 </para>
        /// <para> 创建日期： 2011-11-8</para>
        /// </summary>
        /// <returns></returns>
        public static UserCurrentModel GetUserModel(string userCode = null)
        {
            try
            {
                return UserDal.GetUserModel(userCode);
            }
            catch
            {
                return new UserCurrentModel();
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CallBackModel Register(RegisterModel model)
        {
            var result = new CallBackModel
                {
                    State = 0
                };
            try
            {
                var enty = new UserBase();
                AutoMapperManage.Map(model, enty);
                enty.LogPassword = model.Password;
                enty.LogPasswordMd5 = StringExtension.Md5(model.Password, 32);
                return _userDal.AddUserBase(enty);
            }
            catch (Exception ex)
            {
                result.Message = "注册失败，请联系管理员！";
                result.ErrorMessage = ex.Message;
                return result;
            }
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CallBackModel CheckLogin(LogOnModel model)
        {
            var result = new CallBackModel
               {
                   State = 0,
                   Message = "用户名或者密码错误！"
               };

            try
            {
                model.Password = StringExtension.Md5(model.Password, 32);
                var r = _userDal.CheckLogin(model);
                if (r)
                {
                    result.State = 1;
                    result.Message = "";
                    var usermodel = GetUserModel(model.UserCode);

                }
            }
            catch (Exception ex)
            {
                result.Message = "登录异常，请联系管理员!";
                result.ErrorMessage = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 清除用户Cashe
        /// </summary>
        /// <param name="userCode"></param>
        public static void ClearUserCashe(string userCode = null)
        {
            try
            {
                UserDal.ClearUserCashe(userCode);
            }
            catch { }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CallBackModel ModifyPassword(MobifyPasswordModel model)
        {
            var result = new CallBackModel
                {
                    State = 0
                };
            try
            {
                if (model.ConfirmPassword != model.Password)
                {
                    result.Message = "两次输入的密码不一致！";
                    return result;
                }
                model.Password = StringExtension.Md5(model.Password, 32);
                model.OrigonPassword = StringExtension.Md5(model.OrigonPassword, 32);
                return _userDal.ModifyPassword(model);
            }
            catch (Exception ex)
            {
                result.Message = "密码修改失败，请联系管理员！";
                result.ErrorMessage = ex.Message;
            }
            return result;
        }
    }
}
